<template>
  <el-container style="height: 100vh; display: flex; justify-content: center; align-items: center;">
    <el-card style="width: 400px;">
      <div slot="header" class="clearfix">
        <span style="font-size: 20px; font-weight: bold;">用户注册</span>
      </div>
      <el-form :model="registerForm" :rules="rules" ref="registerForm" label-width="100px">
        <!-- 账号 -->
        <el-form-item label="账号" prop="account">
          <el-input v-model="registerForm.account" placeholder="请输入账号"></el-input>
        </el-form-item>

        <!-- 用户名 -->
        <el-form-item label="昵称" prop="username">
          <el-input v-model="registerForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>

        <!-- 密码 -->
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="registerForm.password" placeholder="请输入密码"></el-input>
        </el-form-item>

        <!-- 确认密码 -->
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input type="password" v-model="registerForm.confirmPassword" placeholder="请再次输入密码"></el-input>
        </el-form-item>

        <!-- 注册按钮 -->
        <el-form-item>
          <el-button type="primary" @click="submitForm('registerForm')">注册</el-button>
          <el-button @click="resetForm('registerForm')">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 跳转到登录页 -->
      <div style="text-align: center;">
        <el-link type="primary" @click="goToLogin">已有账号？去登录</el-link>
      </div>
    </el-card>
  </el-container>
</template>

<script>
import axios from 'axios';
import {handleError, handleSuccess} from "@/util/request";

export default {
  name: "RegisterPage",

  data() {
    // 确认密码校验规则
    const validateConfirmPassword = (rule, value, callback) => {
      if (value !== this.registerForm.password) {
        callback(new Error('两次输入的密码不一致'));
      } else {
        callback();
      }
    };

    return {
      // 表单数据
      registerForm: {
        account: '', // 账号
        username: '', // 用户名
        password: '', // 密码
        confirmPassword: '', // 确认密码
      },

      // 表单校验规则
      rules: {
        account: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 3, max: 16, message: '账号长度在 3 到 16 个字符', trigger: 'blur' }
        ],
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 16, message: '用户名长度在 2 到 16 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '密码长度在 6 到 16 个字符', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { validator: validateConfirmPassword, trigger: 'blur' }
        ]
      }
    };
  },

  methods: {
    // 提交表单
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 发送注册请求
          this.$http.post('/user/register', this.registerForm,{
            headers: {
              'Content-Type': 'application/json' // 设置请求头
            }
          })
              .then((result) => {
                if (handleSuccess(result)){
                  this.$message("注册成功")
                  this.$router.push('/login'); // 跳转到登录页
                }
              })
              .catch(error => {
                console.error('注册失败:', error);
                handleError(error)
              });
        } else {
          console.log('表单验证失败');
          return false;
        }
      });
    },

    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

    // 跳转到登录页
    goToLogin() {
      this.$router.push('/login');
    }
  }
};
</script>

<style scoped>
.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}
</style>